package cn.yunhe.dao.impl;

import cn.yunhe.dao.IEmpDao;
import cn.yunhe.entity.Emp;
import cn.yunhe.util.PageUtil;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("empDao")
public class EmpDaoImpl extends HibernateDaoSupport implements IEmpDao {


    @Override
    public void addEmp(Emp emp) {
        try {
            super.getHibernateTemplate().save(emp);
        } catch (Exception e) {
            throw new RuntimeException("添加失败");
        }finally {
            releaseSession(getSession());
        }
    }

    @Override
    public void delEmp(Emp emp) {
        try {
            super.getHibernateTemplate().delete(emp);
        } catch (Exception e) {
            throw new RuntimeException("删除失败");
        }finally {
            releaseSession(getSession());
        }

    }

    @Override
    public void updateEmp(Emp emp) {
        try {
            super.getHibernateTemplate().update(emp);
        } catch (Exception e) {
            throw new RuntimeException("更新失败");
        }finally {
            releaseSession(getSession());
        }
    }

    @Override
    public Emp getEmpInfoByName(String empname) {

            try {
                Criteria criteria = getSession().createCriteria(Emp.class);
                List<Emp> empList = criteria.add(Restrictions.eq("empname", empname)).list();
                if (empList != null && empList.size() > 0) {
                    return empList.get(0);
                } else {
                    return null;
                }
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }finally {
                releaseSession(getSession());
            }

    }

    @Override
    public List<Emp> getALlEmp(int pageIndex) {

            try {
                Criteria criteria = getSession().createCriteria(Emp.class);
                List<Emp> empList = criteria.setMaxResults(PageUtil.PAGE_SIZE).setFirstResult((pageIndex - 1) * PageUtil.PAGE_SIZE).list();
                return empList;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }finally {
                releaseSession(getSession());
            }
    }

    @Override
    public int countRows() {
            try {
                Criteria criteria = getSession().createCriteria(Emp.class);
                List<Emp> empList = criteria.list();
                return empList.size();
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }finally {
                releaseSession(getSession());
            }
    }

}
